package han.Chensing.CCirnosLibrary.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.crypto.NoSuchPaddingException;

public class NetUtil {

    public static HttpURLConnection getConnection(String path) throws IOException{
        URL url = new URL(path);
        URLConnection urlConnection = url.openConnection();
        HttpURLConnection httpURLConnection=(HttpURLConnection)urlConnection;
        httpURLConnection.setConnectTimeout(3000);
        return httpURLConnection;
    }

    public static ArrayList<String> downloadLinesFromNet(String path)
            throws IOException {
        HttpURLConnection httpURLConnection = getConnection(path);
        String line;
        ArrayList<String> map=new ArrayList<>();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        while ((line=reader.readLine())!=null)
            map.add(line);
        inputStream.close();
        httpURLConnection.disconnect();
        reader.close();
        return map;
    }

    public static ArrayList<String> downloadEncryptedLinesFromNet(String path,String key)
            throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
            InvalidAlgorithmParameterException {
        ArrayList<String> map=new ArrayList<>();
        String line;
        HttpURLConnection httpURLConnection = getConnection(path);
        InputStream inputStream = httpURLConnection.getInputStream();
        InputStream eInputStream = AESUtil.wrapInputStream(inputStream, key);
        BufferedReader reader=new BufferedReader(new InputStreamReader(eInputStream, StandardCharsets.UTF_8));
        while ((line=reader.readLine())!=null)
            map.add(line);
        reader.close();
        eInputStream.close();
        inputStream.close();
        httpURLConnection.disconnect();
        return map;
    }

    public static String downloadFirstLineFromNet(String path) throws IOException{
        ArrayList<String> strings = downloadLinesFromNet(path);
        if (strings.size()<1)
            return null;
        return strings.get(0);
    }

    public static void downloadFileFromNet(String path, File toFile) throws IOException{
        HttpURLConnection httpURLConnection = getConnection(path);
        FileOutputStream fileOutputStream=new FileOutputStream(toFile,true);
        InputStream oriInputStream = httpURLConnection.getInputStream();
        IOUtil.copyStream(oriInputStream,fileOutputStream);
        fileOutputStream.close();
        oriInputStream.close();
        httpURLConnection.disconnect();
    }

    public static void downloadFileFromLocal/*?*/(String path,File toFile) throws IOException{
        FileInputStream fileInputStream=new FileInputStream(path);
        FileOutputStream fileOutputStream=new FileOutputStream(toFile,true);
        IOUtil.copyStream(fileInputStream,fileOutputStream);
        fileInputStream.close();
        fileOutputStream.close();
    }

    /*public static void downloadPartsFileFromNet(String path,int amount,File toFile)
            throws IOException{
        for(int i=0;i<amount;i++)
            downloadFileFromNet(path+""+i,toFile);
    }*/
}
